﻿using DomainService.IService.Sys;
using DomainService.Service.Sys;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model.BusinessModel.Input;
using Model.BusinessModel.OutPut;
using Model.BusinessModel.OutPut.sys;
using OverallAuth_WebApi.PlugInU;
using Utility;

namespace OverallAuth_WebApi.Controllers.OpenApi
{
    /// <summary>
    /// Sys_Button 按钮
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "开放接口")]
    public class OpenApiController : BaseController
    {

        #region 构造实列化

        /// <summary>
        /// 用户服务
        /// </summary>
        public ISysUserService _userService;

        /// <summary>
        /// 登录日志服务
        /// </summary>
        public ISysLoginLogService _sysLoginLogService;


        /// <summary>
        /// 菜单服务服务
        /// </summary>
        public ISysMenuService _sysMenuService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userService">用户服务</param>
        /// <param name="sysLoginLogService">日志服务</param>
        /// <param name="sysMenuService">菜单服务</param>
        public OpenApiController(ISysUserService userService, ISysLoginLogService sysLoginLogService, ISysMenuService sysMenuService)
        {
            _userService = userService;
            _sysLoginLogService = sysLoginLogService;
            _sysMenuService = sysMenuService;
        }

        #endregion

        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous] // 不验证权限
        public ReceiveStatus<LoginOutPut> Login(LoginInput loginModel)
        {
            var result = _userService.GetUserMsg(loginModel.UserName, loginModel.Password, loginModel.CorporationKey, loginModel.SystemKey);
            if (result.success)
            {
                var loginResult = result.data.First();
                var tokenResult = JwtPlugIn.BuildToken(loginResult);
                loginResult.Token = tokenResult.Token;
                loginResult.ExpiresDate = tokenResult.ExpiresDate;
                result.data = new List<LoginOutPut>() { loginResult };
            }

            return result;
        }

        /// <summary>
        /// 获取用户所属菜单
        /// </summary>
        /// <returns>返回用户所属菜单</returns>
        [HttpGet]
        public ReceiveStatus<SysMenuOutPut> GetMenuByUserId()
        {
            var loginModel = GetLoginUserMsg();
            return _sysMenuService.GetMenuByUserId(loginModel.UserId, loginModel.CorporationKey,loginModel.SystemKey);
        }
    }
}
